/*****************************************************************************
模块名  : CDialogPopup
文件名  : dialog_popup.cpp
相关文件:
实现功能: 作为所有二级界面的外框，二级界面嵌入到这个窗口里面，实现统一标题栏样式
作者    :
版本    : BoLight-V1.0-R1.0 Copyright (C) 2023 - 2055 All Rights Reserved
==============================================================================*/

#include <QBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QSpacerItem>
#include <QMouseEvent>
#include <QTextStream>
#include "dialog_popup.h"

CDialogPopup::CDialogPopup(QWidget* parent)
 :QDialog(parent)
{
    Init_SystemTitleBar();
    Init_Interface();
}

CDialogPopup::~CDialogPopup()
{

}

void CDialogPopup::LoadStyleSheet()
{
    QFile file(":/style/stylesheet_dialogPopup.qss");
    if (file.open(QFile::ReadOnly)) {
        QTextStream stream(&file);
        QString styleSheet = stream.readAll();
        setStyleSheet(styleSheet);
        file.close();
    }
}

//////////////////////////////////////////////////////////////////////////
//
void CDialogPopup::Init_SystemTitleBar()
{
    setWindowModality(Qt::ApplicationModal);  // 设置为应用程序模态
    setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
    setWindowTitle(QString::fromUtf8(""));
    setWindowFlags(Qt::FramelessWindowHint);  //隐藏边框，标题栏属于边框
}

void CDialogPopup::Init_Interface()
{
    // 1.设置窗体的最小尺寸和名称
    setMinimumSize(800,600);
    setObjectName(QString::fromUtf8("dlg_popup"));

    // 2.设置主窗体的垂直布局
    m_VWgtLayout = new QVBoxLayout(this);
    m_VWgtLayout->setMargin(0);
    m_VWgtLayout->setSpacing(0);
    m_VWgtLayout->setContentsMargins(0, 0, 0, 0);

    // 3.创建界面里的主要的几个功能区域框
    m_titleFrame = new QFrame(this);
    m_titleFrame->setObjectName(QString::fromUtf8("titleFrame"));
    m_titleFrame->setFixedHeight(50);

    m_btnFrame = new QFrame(this);
    m_btnFrame->setObjectName(QString::fromUtf8("btnFrame"));
    m_btnFrame->setFixedHeight(60);

//    QFrame *pLine = new QFrame(m_titleFrame);
    QFrame *pLine = new QFrame(this);
    pLine->setObjectName(QString::fromUtf8("line"));
    pLine->setMinimumSize(QSize(0, 1));
    pLine->setMaximumSize(QSize(16777215, 1));
    pLine->setFrameShape(QFrame::HLine);
    pLine->setFrameShadow(QFrame::Sunken);

    // 4.向标题栏区域框内添加组件
    m_lbTitle = new QLabel(m_titleFrame);
    m_lbTitle->setObjectName(QString::fromUtf8("title"));
    m_lbTitle->setFixedSize(80,30);

    m_pbClose = new QPushButton(m_titleFrame);
    m_pbClose->setObjectName(QString::fromUtf8("pb_close"));
    m_pbClose->setFixedSize(30,30);
    connect(m_pbClose, &QPushButton::clicked, this, &CDialogPopup::onButton_Close);

    QHBoxLayout* hTitleLayout = new QHBoxLayout(m_titleFrame);
    hTitleLayout->setSpacing(0);
    hTitleLayout->setContentsMargins(22, 10, 22, 10);
    hTitleLayout->addWidget(m_lbTitle);
    hTitleLayout->addStretch(1);  //添加一个QSpacerItem
    hTitleLayout->addWidget(m_pbClose);

//    QVBoxLayout* vWgtLayout = new QVBoxLayout(m_titleFrame);
//    vWgtLayout->addLayout(hTitleLayout);
//    vWgtLayout->setContentsMargins(22, 10, 22, 10);
//    m_titleFrame->setLayout(vWgtLayout);

    // 5.向按钮区域框内添加组件
    QSpacerItem* hSpacer2 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    m_pbOk = new QPushButton(m_btnFrame);
    m_pbOk->setObjectName(QString::fromUtf8("pb_ok"));
    m_pbOk->setFixedSize(80,30);
    m_pbOk->setText(QString::fromUtf8("确定"));
    connect(m_pbOk, &QPushButton::clicked, this, &CDialogPopup::onButton_Ok);

    m_pbCancel = new QPushButton(m_btnFrame);
    m_pbCancel->setObjectName(QString::fromUtf8("pb_cancel"));
    m_pbCancel->setFixedSize(80,30);
    m_pbCancel->setText(QString::fromUtf8("取消"));
    connect(m_pbCancel, &QPushButton::clicked, this, &CDialogPopup::onButton_Cancel);

    QHBoxLayout* hBtnLayout = new QHBoxLayout(m_btnFrame);
    hBtnLayout->setSpacing(40);
    hBtnLayout->setContentsMargins(10, 10, 60, 10);
    hBtnLayout->addStretch(1);
    hBtnLayout->addWidget(m_pbOk);
    hBtnLayout->addWidget(m_pbCancel);

    // 6.向主窗体布局里添加区域框
    m_VWgtLayout->addWidget(m_titleFrame);
    m_VWgtLayout->addWidget(pLine);
    m_VWgtLayout->addWidget(m_btnFrame);
}

void CDialogPopup::SetTitleName(QString qstrTitle)
{
    m_lbTitle->setText(qstrTitle);
}

void CDialogPopup::SetStyle()
{
    QString strStyleSheet;

}

void CDialogPopup::AddWidget(QWidget* widget)
{
    m_VWgtLayout->insertWidget(2,widget);
}

//////////////////////////////////////////////////////////////////////////
//
void CDialogPopup::resizeEvent(QResizeEvent* event)
{
    LoadStyleSheet();
}

void CDialogPopup::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_nOffset = event->globalPos() - pos();
    }
}

void CDialogPopup::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        QPoint eventPos = event->globalPos();
        QPoint movePos = eventPos - m_nOffset;
        move(movePos);
    }
}

//为了在不同分辨率的显示器上拖动切换时可以自动调整对话框界面。切换显示器会触发paintEvent事件
void CDialogPopup::paintEvent(QPaintEvent *event)
{
    //sizeHint()函数会自动调节里面的窗体的尺寸
    resize(m_VWgtLayout->sizeHint());
}

//////////////////////////////////////////////////////////////////////////
//
void CDialogPopup::onButton_Close()
{
    emit sglBtn_Close();
    close();
}

void CDialogPopup::onButton_Ok()
{
    emit sglBtn_Ok();
}

void CDialogPopup::onButton_Cancel()
{
    emit sglBtn_Cancel();
    close();
}
